<template>
	<section>
		<!--  工具条 -->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getListData" >查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="showAdd" >新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--  列表 -->
		<el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="colsChange" style="width: 100%;">
			<el-table-column type="selection" width="50">
			</el-table-column>
			<el-table-column type="index" width="30" fixed>
			</el-table-column>
			<el-table-column prop="name" label="明细名称" fixed sortable>
			</el-table-column>
			<el-table-column prop="requence" label="序号" sortable>
			</el-table-column>
			<el-table-column prop="intro" label="简介"   sortable>
			</el-table-column>
			<el-table-column prop="dictionary.name" label="所属字典"  width="150" sortable>
			</el-table-column>
			<el-table-column label="操作" width="160" fixed="right" align="center">
				<template scope="scope">
					<el-button size="small" @click="showEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="delSubmit(scope.$index, scope.row)" >删除</el-button>
				</template>
			</el-table-column>

		</el-table>

		<!--  编辑界面 -->
		<el-dialog title="编辑" v-model="editFormOn" :close-on-click-modal="false" align="left">
			<el-form :model="editForm2" label-width="80px" :rules="editForm1" ref="editForm2">
				<el-form-item label="字典明细名称" prop="planType_id">
					<el-input v-model.number="editForm2.name" auto-complete="off" style="width: 50%"></el-input>
				</el-form-item>
				<el-form-item label="字典选择" prop="potentialCustomer_idName" >
					<el-select v-model.number="editForm2.dictionary_id" placeholder="请选择" >
						<el-option
								v-for="item in dictionary"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="简介" prop="planDetails">
					<el-input v-model="editForm2.intro" type="textarea" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="序号" prop="planType_id">
					<el-input v-model.number="editForm2.requence" auto-complete="off" style="width: 50%"></el-input>
				</el-form-item>>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormOn = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--  新增界面 -->
		<el-dialog title="新增" v-model="addFormOn" :close-on-click-modal="false" align="left">
			<el-form :model="addForm2" label-width="80px" :rules="addForm1" ref="addForm2">
				<el-form-item label="字典明细名称" prop="planType_id">
					<el-input v-model.number="addForm2.name" auto-complete="off" style="width: 50%"></el-input>
				</el-form-item>
				<el-form-item label="字典选择" prop="potentialCustomer_idName" >
					<el-select v-model.number="addForm2.dictionary_id" placeholder="请选择" >
						<el-option
								v-for="item in dictionary"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="简介" prop="planDetails">
					<el-input v-model="addForm2.intro" type="textarea" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="序号" prop="planType_id">
					<el-input v-model.number="addForm2.requence" auto-complete="off" style="width: 50%"></el-input>
				</el-form-item>>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormOn = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--  工具条 -->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.cols.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="showCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
	</section>
</template>

<script>
    import util from '../../../common/js/util';
    import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../../api/api';

    export default {
        data() {
            return{
                filters: {
                    name: ''
                },
                listData: [],
                keyword:'',
                pageSize: 10,
                currentPage:1,
                listLoading : false,

                //列表选中的列
                cols: [],

				//潜在用户的下拉框
                cusSourceLoading:false,
				dictionary:[],

                //新增界面是否显示
                addFormOn: false,
                //新增等待界面
                addLoading: false,
              //新增表格
                addForm1: {
                },
                //新增界面空表格
                addForm2: {
					dictionary_id:'',
					sn:'',
					name:'',
					requence:'',
					intro:'',
					dictionary:[]
                },

                //编辑界面是否显示
                editFormOn: false,
                //编辑等待界面
                editLoading: false,
                //编辑表格
               editForm1: {

                },
                //编辑界面空表格
                editForm2: {
					dictionary_id:'',
					name:'',
					requence:'',
					dictionary:[]
                },
			}
        },
        methods: {

            //分页查询 + 高级查询
            getListData() {
				let para = {
                    currentPage: this.currentPage,
                    pageSize:this.pageSize,
                    keyword: this.filters.name
                };
                this.listLoading = true;
                this.$http.post('/dictoryDetails/findByPage',para).then(res=>{
                    let ajaxResult =  res.data;
                    if (ajaxResult.success){
                        this.listData = ajaxResult.data.result;
                        this.total = ajaxResult.data.total
                    }else {
                        this.$message({
                            message: '加载失败',
                            type: 'error'
                        });
                    }
                    this.listLoading = false;
                }).catch(error=>{
                    this.listLoading = false;
                    this.$message({
                        message: '加载失败',
                        type: 'error'
                    });
                })
            },

            //新增的界面
            showAdd:function () {

                this.addFormOn = true;
                this.addForm2 = {

                };
            },
            //新增的方法
            addSubmit:function () {
                this.$refs.addForm2.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = Object.assign({}, this.addForm2);
                            console.debug(para);
                            this.$http.post('/dictoryDetails/insert/'+para.dictionary_id,para).then(res =>{
                                let ajaxResult = res.data;
                                if (ajaxResult.success){
                                    this.$message.success("添加成功");
                                    this.addFormOn = false;
                                    this.$refs['addForm2'].resetFields();
                                    this.getListData();
                                }else {
                                    this.addFormOn = false;
                                    this.$message({
                                        message: '添加失败',
                                        type: 'error'
                                    });
                                }
                                this.addLoading = false;
                            }).catch(error =>{
                                this.addLoading = false;
                                this.$message({
                                    message: '添加失败',
                                    type: 'error'
                                });
                            })
                        });
                    }
                });
            },

            //删除
            delSubmit: function (index, row) {
                this.$confirm('确认删除吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete('/dictoryDetails/delete/'+row.id,{}).then(res=>{
                        let ajaxResult =  res.data
                        if (ajaxResult.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getListData();
                        }else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                        this.listLoading = false;
                    }).catch(error=>{
                        this.listLoading = false;
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        });
                    })

                });
            },

            //修改的界面
            showEdit:function (index, row) {
                this.editFormOn = true;
                this.editForm2 = Object.assign({}, row);
                this.editForm2.dictionary_id = row.dictionary.id;
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm2.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm2);
                            this.$http.post("/dictoryDetails/updateAll/"+para.dictionary_id,para).then(res =>{
                                let ajaxResult = res.data
                                if (ajaxResult.success){
                                    this.$message({
                                        message:"修改成功",
                                        type:'success'
                                    })
                                    this.$refs['editForm2'].resetFields();
                                    this.editFormOn = false;
                                    this.editLoading = false;
                                    this.getListData();
                                }else {
                                    this.editLoading = false;
                                    this.message({
                                        message:"修改失败",
                                        type:'error'
                                    })

                                }
                            }).catch(error=>{
                                this.editLoading = false;
                                this.$message({
                                    message:"修改失败",
                                    type:'success'
                                })
                            })
                        });
                    }
                });
            },

            //显示当前的页数
            showCurrentChange(val) {
                this.currentPage=val;
                this.getListData();
            },

            colsChange: function (cols) {
                this.cols = cols;
            },

            //批量删除
            batchRemove: function () {
                var ids = this.cols.map(item => item.id)
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = { ids: ids };
                    this.$http.post("/dictoryDetails/batchDelete",para).then(res=>{
                        let ajaxResult = res.data;
                        if (ajaxResult.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getListData();
                        }else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                        this.listLoading = false;
                    }).catch(error => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        });
                    })

                })
            },

			loadPotCus:function () {

                this.cusSourceLoading = true;
                this.$http.get('/dictionary/all').then(res=>{
                    let ajaxResult =  res.data;
                    if (ajaxResult.success){
                        this.dictionary = ajaxResult.data;
                        this.total = ajaxResult.data.total;
                    }else {
                        this.$message({
                            message: '加载失败',
                            type: 'error'
                        });
                    }
                    this.cusSourceLoading = false;
                }).catch(error=>{
                    this.cusSourceLoading = false;
                    this.$message({
                        message: '加载失败',
                        type: 'error'
                    });
                })
            },


        },
        mounted() {
            this.getListData();
            this.loadPotCus();
        }
    }

</script>

<style scoped>

</style>